package com.zzyl.serve.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.core.domain.R;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.vo.NursingPlanVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 护理计划Controller
 *
 * @author itheima
 * @date 2025-03-03
 */
@RestController
@RequestMapping("/serve/plan")
@Api(tags =  "护理计划相关接口")
public class NursingPlanController extends BaseController
{
    @Autowired
    private INursingPlanService nursingPlanService;
    @Autowired
    private RedisTemplate redisTemplate;
    //redis KEY的常量
    private static final String KEY_NURSING_PLAN_ALL="nursingPlan:all";
    /**
     * 查询护理计划列表
     */
    @PreAuthorize("@ss.hasPermi('serve:plan:list')")
    @GetMapping("/list")
    @ApiOperation("查询护理计划列表")
    public TableDataInfo<List<NursingPlan>> list(NursingPlan nursingPlan)
    {
        startPage();
        List<NursingPlan> list = nursingPlanService.selectNursingPlanList(nursingPlan);
        return getDataTable(list);
    }

    /**
     * 导出护理计划列表
     */
    @PreAuthorize("@ss.hasPermi('serve:plan:export')")
    @Log(title = "护理计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出护理计划列表")
    public void export(HttpServletResponse response, NursingPlan nursingPlan)
    {
        List<NursingPlan> list = nursingPlanService.selectNursingPlanList(nursingPlan);
        ExcelUtil<NursingPlan> util = new ExcelUtil<NursingPlan>(NursingPlan.class);
        util.exportExcel(response, list, "护理计划数据");
    }

    /**
     * 获取护理计划详细信息
     *
     * 3.2 改造为多表关联查询
     */
    @PreAuthorize("@ss.hasPermi('serve:plan:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取护理计划详细信息")
    public R<NursingPlanVo> getInfo(@ApiParam(value = "护理计划ID", required = true)
                                  @PathVariable("id") Integer id)
    {
        return R.ok(nursingPlanService.selectNursingPlanById(id));
    }



    /**
     * 新增护理计划
     *
     * 3.2 改造了 接收参数的类型为DTO
     */
    @PreAuthorize("@ss.hasPermi('serve:plan:add')")
    @Log(title = "护理计划", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增护理计划")
//    public AjaxResult add(@ApiParam(value = "护理计划实体", required = true) @RequestBody NursingPlan nursingPlan)
    public AjaxResult add(@ApiParam(value = "护理计划实体", required = true) @RequestBody NursingPlanDto nursingPlanDto)
    {
        // 删除缓存
        deleteCache();
        return toAjax(nursingPlanService.insertNursingPlan(nursingPlanDto));
    }



    /**
     * 修改护理计划
     */
    @PreAuthorize("@ss.hasPermi('serve:plan:edit')")
    @Log(title = "护理计划", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改护理计划")
    public AjaxResult edit(@ApiParam(value = "护理计划实体", required = true)  @RequestBody NursingPlanDto nursingPlanDto)
    {
        // 删除缓存
        deleteCache();
        return toAjax(nursingPlanService.updateNursingPlan(nursingPlanDto));
    }





    /**
     * 删除护理计划
     */
    @PreAuthorize("@ss.hasPermi('serve:plan:remove')")
    @Log(title = "护理计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @ApiOperation("删除护理计划")
    public AjaxResult remove(@PathVariable Integer id)
    {
        // 删除缓存
        deleteCache();
        return toAjax(nursingPlanService.deleteNursingPlanById(id));
    }


    /**
     * 查询所有护理计划
     */
    @GetMapping("/all")
    @ApiOperation(value = "获取所有护理计划")
    public R<List<NursingPlan>> listAll()
    {
        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingPlan::getStatus, 1);

        //1.先查询缓存
        List<NursingPlan> list = (List<NursingPlan>) redisTemplate.opsForValue().get(KEY_NURSING_PLAN_ALL);
        //2.断缓存是否存在
        if (list != null && list.size() > 0) {
            //如果存在直接返回
            return R.ok(list);
        }

        List<NursingPlan> planList = nursingPlanService.list(queryWrapper);
        //3.不存在，查询数据库，并且存入缓存
        redisTemplate.opsForValue().set(KEY_NURSING_PLAN_ALL, planList);
        return R.ok(planList);
    }
    private void deleteCache() {
        // 删除缓存
        redisTemplate.delete(KEY_NURSING_PLAN_ALL);
    }
}